<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/6/25 11:36
 * Modified Time: 2019/6/25 11:36
 * Version: 2.0
 */

namespace App\Models;


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class UserStore extends Model
{
    protected $table = 'user_store';

    public $timestamps= true;

    protected $guarded=[];


    public function role(){
    	return $this->belongsTo(Role::class,'rid','id');
    }

    public function user(){
    	return $this->belongsTo(User::class,'uid','id');
    }


    public function userPortfolio(){
    	return $this->hasMany(UserPortfolio::class,'user_id','uid');
    }

    public function store(){
        return $this->belongsTo(Store::class,'sid','id');
    }


	public function scopeStoreIdUserIdBy(Builder $query, $storeId,$roleType){
		return $query->has('store','and',function($q) use($storeId,$roleType){
			$q->where(['sid'=>$storeId,'rid'=>$roleType]);
		});
	}



    public function scopeStoreCodeBy(Builder $query,$adcode){
	    return $query->has('store','and',function($q) use($adcode){
		    $q->where('adcode',$adcode);
	    });
    }

	public function scopeStoreBy(Builder $query, $storeId){
		return $query->has('store','and',function($q) use($storeId){
			$q->where(['sid'=>$storeId]);
		});
	}


	public function scopeWithCertain($query, $relation, Array $columns){
		return $query->with([$relation => function ($query) use ($columns){
			$query->select(array_merge(['id'], $columns));
		}]);
	}

    public function order(){
        return $this->hasMany(Order::class,'barber_id','uid');
    }
}